<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/15/14
 * Time: 10:13 PM
 */

namespace Admin\Model;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Admin\Model\AdminUserMessage;

class AdminUserMessageTable {
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll($user_id)
    {

        //get all message of user
        $select = new Select('user_message');
        $select->where(array('to_user = '.$user_id, 'is_accepted'=>'0' ));
        $select->order('create_date DESC');
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new AdminUserMessage());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }

    public function fetchNew($user_id)
    {

        //get all message of user
        $select = new Select('user_message');
        $select->where(array('to_user = '.$user_id,'is_accepted = 0'));

        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new AdminUserMessage());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);

        return $paginator;
    }
    public function getMessageByUser($userId,$from,$to){

        $str = 'SELECT * FROM user_message WHERE `to_user` = '.$userId.' ORDER BY create_date DESC limit '.$from.' ,'.$to;

        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($str);
        $statement = $result->getResource();
        $resultSet1 = $statement->fetchAll(\Pdo::FETCH_NAMED);
        // $resultSet1 = $statement->fetchAll(\Pdo::FETCH_OBJ);
        return $resultSet1;
    }

    public function saveAddMessage(AdminUserMessage $userMessage)
    {
        $data = array(

            'form_user' => $userMessage->form_user,
            'form_user_email' => $userMessage->form_user_email,
            'from_user_name' => $userMessage->from_user_name,

            'to_user' => $userMessage->to_user,
            'to_user_name' => $userMessage->to_user_name,
            'form_user_email' => $userMessage->form_user_email,

            'is_accepted' => $userMessage->is_accepted,
            'message_content' => $userMessage->message_content,
            'type_request' => $userMessage->type_request,
            'create_date' => $userMessage->create_date,


        );

        $id = (int)$userMessage->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
            return true;
        } else {
            if ($this->getMessage($id)) {
                $this->tableGateway->update($data, array('id' => $id));
                return true;
            } else {
                return false;
            }
        }
        return false;

    }

    public function getMessage($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();

        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function deleteUserMessage($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }

    public function fetchFriendMessage($user_id, $friend, $from_row, $to_row){
        if($from_row == null && $to_row == null){
            $from_row = 0;
            $to_row = 1000;
        }


        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute('CALL fetch_user_message('.$user_id.','.$friend.','.$from_row.','.$to_row.')');

        $statement = $result->getResource();
        $resultSet1 = $statement->fetchAll(\Pdo::FETCH_NAMED);
        // $resultSet1 = $statement->fetchAll(\Pdo::FETCH_OBJ);
        return $resultSet1;
    }
}